<template>
  <div class="oss-upload-container">
    <el-upload
      action
      class="avatar-uploader"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>


<script>
import OSS from "ali-oss";

export default {
  name: "ossUpload",
  props: {
    imgName: {
      type: String,
      default: "test"
    },
    // 已有图片，需要传图片地址
    imageUrl: {
      type: String,
      default: ""
    }
  },
  data() {
    return {};
  },
  methods: {
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
        return;
      }
      this.toUpload(file);
      return false;
    },
    /**
     * oss上传
     * @param file
     */
    toUpload(file) {
      // oss配置
      const client = new OSS({
        endpoint: "oss-cn-beijing.aliyuncs.com",
        accessKeyId: "LTAI4FgrsFXo8ij335BxeRbp", // 已泄露LTAIhKopVjzlM2aM
        accessKeySecret: "oG0PWkV0qOeDoJQfwEHOqIvTp16S0x",
        bucket: "stavebox"
      });
      let imgType = file.type.split("/")[1];
      let fileName = this.imgName + "." + imgType;
      // 将文件流上传至oss
      client
        .put(fileName, file)
        .then(res => {
          const imageUrl = res.name;
          this.$emit("upload-img", imageUrl);
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>
<style lang="scss">
.oss-upload-container {
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 18px;
    color: #8c939d;
    width: 64px;
    height: 64px;
    line-height: 64px;
    text-align: center;
  }

  .avatar {
    width: 64px;
    height: 64px;
    display: block;
  }
}
</style>